package GUI.main.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import GUI.main.PatientListRenderer;
import OfficeServer.users.Patient;
import OfficeServer.users.User;
import OfficeServer.users.UserList;

/**
 * Doctor specific navigation panel to be placed into nav panel.
 * @author mramsey3
 *
 */
/**
 * @author mramsey3
 *
 */
public class DoctorNavigationMenuPanel extends JPanel implements MouseListener{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private JButton viewWaitingAppointments;
    private JPanel patientPanel;
    private JPanel patientTitlePanel;
    private JPanel patientButtonPanel;
    private JButton createPatient;
    private JButton deletePatient;
    private JScrollPane patientScrollPane;
    private JList patientList;
    
    /**
     * 
     */
    public DoctorNavigationMenuPanel(){
	super();
	setLayout(new BorderLayout());
	add(getViewWaitingAppointments(),BorderLayout.SOUTH);
	add(getPatientPanel(),BorderLayout.CENTER);
    }
    
    /**
     * @author mramsey3
     * @return
     */
    private JButton getViewWaitingAppointments() {
	if(viewWaitingAppointments == null){
	    viewWaitingAppointments = new JButton("View Waiting Appointments");
	    viewWaitingAppointments.setPreferredSize(new Dimension(150,25));
	    viewWaitingAppointments.setMargin(new Insets(0,0,0,0));
	    viewWaitingAppointments.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
	    viewWaitingAppointments.putClientProperty("JButton.buttonType", "textured");
	    viewWaitingAppointments.addMouseListener(this);
	}
        return viewWaitingAppointments;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientPanel() {
	if(patientPanel == null){
	    patientPanel = new JPanel();
	    patientPanel.setPreferredSize(new Dimension(200,450));
	    patientPanel.setLayout(new BorderLayout());
	    patientPanel.setBorder(BorderFactory.createBevelBorder(0));
	    patientPanel.add(getPatientTitlePanel(), BorderLayout.NORTH);
	    patientPanel.add(getPatientScrollPane(), BorderLayout.CENTER);
	    patientPanel.add(getPatientButtonPanel(), BorderLayout.SOUTH);
	    patientPanel.addMouseListener(this);
	}
	return patientPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientTitlePanel() {
	patientTitlePanel = new JPanel(){
		private static final long serialVersionUID = 1L;

		/**
		 * custom paintCompontent for friendTitlePanel.
		 * @param g system Graphics
		 */
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			g.setColor(new Color(100,150,220));
			g.fillRect(0,0,getWidth(),getHeight());
			g.setColor(Color.white);
			g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
			g.drawString("PATIENTS:", 10, getHeight()-8);
		}
	};
	patientTitlePanel.setPreferredSize(new Dimension(200,25));
	patientTitlePanel.addMouseListener(this);
	
        return patientTitlePanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientButtonPanel() {
	if(patientButtonPanel == null){
	    patientButtonPanel = new JPanel();
	    patientButtonPanel.setPreferredSize(new Dimension(200,30));
	    patientButtonPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    patientButtonPanel.add(getCreatePatient(), gbc1);
		
	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    patientButtonPanel.add(Box.createHorizontalStrut(2), gbc2);
		
	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    patientButtonPanel.add(getDeletePatient(), gbc3);
	    patientButtonPanel.addMouseListener(this);
	}
        return patientButtonPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JButton getCreatePatient() {
	if(createPatient == null){
	    createPatient = new JButton("Create");
	    createPatient.setPreferredSize(new Dimension(75, 25));
	    createPatient.setMargin(new Insets(0,0,0,0));
	    createPatient.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
	    createPatient.putClientProperty("JButton.buttonType", "textured");
	    createPatient.addMouseListener(this);
	}
        return createPatient;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JButton getDeletePatient() {
	if(deletePatient == null){
	    deletePatient = new JButton("Delete");
	    deletePatient.setPreferredSize(new Dimension(75, 25));
	    deletePatient.setMargin(new Insets(0,0,0,0));
	    deletePatient.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
	    deletePatient.putClientProperty("JButton.buttonType", "textured");
	    deletePatient.addMouseListener(this);
	}
        return deletePatient;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JScrollPane getPatientScrollPane() {
	if(patientScrollPane == null){
	    patientScrollPane = new JScrollPane();
	    patientScrollPane.setPreferredSize(new Dimension(200,0));
	    patientScrollPane.setViewportView(getPatientList());
	}
        return patientScrollPane;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JList getPatientList() {
	if(patientList == null){
	    patientList = new JList(new PatientListModel());
	    patientList.setCellRenderer(new PatientListRenderer());
	    patientList.addMouseListener(new DoubleClickListener(patientList));
	}
        return patientList;
    }

    /**
     * @author mramsey3
     * @param p
     */
    public void refreshPanel(Patient p){
	patientList.updateUI();
	repaint();
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mouseEntered(MouseEvent e) {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mouseExited(MouseEvent e) {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mousePressed(MouseEvent e) {
	// TODO Auto-generated method stub
	
    }

    @Override
    public void mouseReleased(MouseEvent e) {
	// TODO Auto-generated method stub
	
    }

    /**
     * @author mramsey3
     *
     */
    private class PatientListModel extends AbstractListModel {
	private static final long serialVersionUID = 1L;
	
	
	public Object getElementAt(int i){
		return UserList.getUserList().get(i);
	}
	
	public int getSize(){
		return UserList.getUserList().size();
	}
}
    /**
     * @author mramsey3
     *
     */
    private class DoubleClickListener extends MouseAdapter {
	protected JList list;
	
	/**
	 * @param l
	 */
	public DoubleClickListener(JList l){
		list = l;
	}
	
	public void mouseClicked(MouseEvent e){
		if(e.getSource() == patientList)
		if (e.getClickCount() == 2){
			if(patientList.getModel().getSize()>0){
				int index = list.locationToIndex(e.getPoint());
				ListModel lm = list.getModel();
				Patient p = (Patient)(lm.getElementAt(index));
				refreshPanel(p);
			}
		}
	}
}
}
